/*-----------------------------------------------------------------------*/
/* Low level disk I/O module skeleton for FatFs     (C)ChaN, 2013        */
/*-----------------------------------------------------------------------*/
/* If a working storage control module is available, it should be        */
/* attached to the FatFs via a glue function rather than modifying it.   */
/* This is an example of glue functions to attach various exsisting      */
/* storage control module to the FatFs module with a defined API.        */
/*-----------------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "NuMicro.h"
#include "diskio.h"     /* FatFs lower layer API */
#include "ff.h"

#define SDH0_DRIVE      0        /* for SD0          */
#define SDH1_DRIVE      1        /* for SD1          */
#define EMMC_DRIVE      2        /* for eMMC/NAND    */
#define USBH_DRIVE_0    3        /* USB Mass Storage */
#define USBH_DRIVE_1    4        /* USB Mass Storage */
#define USBH_DRIVE_2    5        /* USB Mass Storage */
#define USBH_DRIVE_3    6        /* USB Mass Storage */
#define USBH_DRIVE_4    7        /* USB Mass Storage */


/*-----------------------------------------------------------------------*/
/* SD CARD DRIVER                                                        */
/*-----------------------------------------------------------------------*/
DSTATUS sd_disk_initialize (BYTE pdrv);
DSTATUS sd_disk_status (BYTE pdrv);
DRESULT sd_disk_read (BYTE pdrv, BYTE *buff, DWORD sector, UINT count);
DRESULT sd_disk_write (BYTE pdrv, const BYTE *buff, DWORD sector, UINT count);
DRESULT sd_disk_ioctl (BYTE pdrv, BYTE cmd, void *buff );

DSTATUS usb_disk_initialize (BYTE pdrv);
DSTATUS usb_disk_status (BYTE pdrv);
DRESULT usb_disk_read (BYTE pdrv, BYTE *buff, DWORD sector, UINT count);
DRESULT usb_disk_write (BYTE pdrv, const BYTE *buff, DWORD sector, UINT count);
DRESULT usb_disk_ioctl (BYTE pdrv, BYTE cmd, void *buff );


/*-----------------------------------------------------------------------*/
/* Initialize a Drive                                                    */
/*-----------------------------------------------------------------------*/

DSTATUS disk_initialize (BYTE pdrv)       /* Physical drive number (0..) */
{

	if(pdrv <= 1)
	{
		return sd_disk_initialize(pdrv);
	}
	else
	{
		return usb_disk_initialize(pdrv);
	}
	
	return RES_PARERR;
}


/*-----------------------------------------------------------------------*/
/* Get Disk Status                                                       */
/*-----------------------------------------------------------------------*/

DSTATUS disk_status (BYTE pdrv)       /* Physical drive number (0..) */
{
	if(pdrv <= 1 )
	{
		return sd_disk_status(pdrv);
	}
	else
	{
		return usb_disk_status(pdrv);
	}
	
	return RES_PARERR;
}



/*-----------------------------------------------------------------------*/
/* Read Sector(s)                                                        */
/*-----------------------------------------------------------------------*/

DRESULT disk_read (
    BYTE pdrv,      /* Physical drive number (0..) */
    BYTE *buff,     /* Data buffer to store read data */
    DWORD sector,   /* Sector address (LBA) */
    UINT count      /* Number of sectors to read (1..128) */
)
{
	if(pdrv <= 1)
	{
		return sd_disk_read(pdrv, buff, sector, count);
	}
	else
	{
		return usb_disk_read(pdrv, buff, sector, count);
	}
	
	return RES_PARERR;
}



/*-----------------------------------------------------------------------*/
/* Write Sector(s)                                                       */
/*-----------------------------------------------------------------------*/

DRESULT disk_write (
    BYTE pdrv,          /* Physical drive number (0..) */
    const BYTE *buff,   /* Data to be written */
    DWORD sector,       /* Sector address (LBA) */
    UINT count          /* Number of sectors to write (1..128) */
)
{
	if(pdrv <= 1)
	{
		return sd_disk_write(pdrv, buff, sector, count);
	}
	else
	{
		return usb_disk_write(pdrv, buff, sector, count);
	}
	
	return RES_PARERR;
}


/*-----------------------------------------------------------------------*/
/* Miscellaneous Functions                                               */
/*-----------------------------------------------------------------------*/

DRESULT disk_ioctl (
    BYTE pdrv,      /* Physical drive number (0..) */
    BYTE cmd,       /* Control code */
    void *buff      /* Buffer to send/receive control data */
)
{
	if(pdrv <= 1)
	{
		return sd_disk_ioctl(pdrv, cmd, buff);
	}
	else
	{
		return usb_disk_ioctl(pdrv, cmd, buff);
	}
	
	return RES_PARERR;
}
